package com.java.comprehensive.arithmetic.leecode.easy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 窦绍飞
 * @Date: 2022/6/12
 * 买卖股票的最佳时机
 */
public class LeetCode_121 {
	public static int maxProfit(int[] prices) {
		int maxprofit = 0;
		for (int i = 0; i < prices.length - 1; i++) {
			for (int j = i + 1; j < prices.length; j++) {
				int profit = prices[j] - prices[i];
				if (profit > maxprofit) {
					maxprofit = profit;
				}
			}
		}
		return maxprofit;
	}

	public static int maxProfit_2(int[] prices) {
		int minprice = Integer.MAX_VALUE;
		int maxprofit = 0;
		for (int i = 0; i < prices.length; i++) {
			if (prices[i] < minprice) {
				minprice = prices[i];
			} else if (prices[i] - minprice > maxprofit) {
				maxprofit = prices[i] - minprice;
			}
		}
		return maxprofit;
	}

	public static void main(String[] args) {
		int[] nums = {7, 1, 5, 3, 6, 4};
		maxProfit(nums);
	}
}
